package com.dks.repository.impl;

import com.dks.mapper.TalksRecordMapper;
import com.dks.mapper.TimesMapper;
import com.dks.models.TalksRecord;
import com.dks.models.Times;
import com.dks.repository.ITimesRepository;
import com.dks.result.ResultModel;
import com.dks.util.Commond;
import com.dks.util.PageInfo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class TimesRepository implements ITimesRepository {

    @Resource
    private TimesMapper timesMapper;

    @Resource
    private TalksRecordMapper talksRecordMapper;


    @Override
    public ResultModel<Boolean> insert(Times times) {
        int row = timesMapper.insert(times);
        ResultModel resultModel = new ResultModel();
        if (1 == row) {
            resultModel.setCode(Commond.CODE_SUCCESS);
            resultModel.setMessage(Commond.MSG_SUCCESS);
            resultModel.setData(row);
        } else {
            resultModel.setCode(Commond.CODE_FAIL);
            resultModel.setMessage(Commond.MSG_FALE);
            resultModel.setData(row);
        }
        return resultModel;
    }

    @Override
    public ResultModel<Boolean> update(Times times) {
        int row = timesMapper.update(times);
        ResultModel resultModel = new ResultModel();
        if (1 == row) {
            resultModel.setCode(Commond.CODE_SUCCESS);
            resultModel.setMessage(Commond.MSG_SUCCESS);
            resultModel.setData(row);
        } else {
            resultModel.setCode(Commond.CODE_FAIL);
            resultModel.setMessage(Commond.MSG_FALE);
            resultModel.setData(row);
        }
        return resultModel;
    }

    @Override
    public ResultModel selectById(Integer id) {
        ResultModel resultModel = new ResultModel();
        Times Times = timesMapper.selectById(id);
        resultModel.setCode(Commond.CODE_SUCCESS);
        resultModel.setMessage(Commond.MSG_SUCCESS);
        resultModel.setData(Times);
        return resultModel;
    }

    @Override
    public ResultModel<PageInfo<Times>> selectList(Integer pageNum, Integer pageSize, String beginTime) {
        ResultModel resultModel = new ResultModel();
        Map<String, Object> map = new HashMap<>();
        map.put("beginTime", beginTime + " 00:00:00");
        Page p = PageHelper.startPage(pageNum, pageSize);
        List<Times> Timess = timesMapper.selectList(map);
        PageInfo<Times> pageInfo = new PageInfo<>(pageNum, pageSize, p.getTotal(), p.getPages(), Timess);
        resultModel.setCode(Commond.CODE_SUCCESS);
        resultModel.setMessage(Commond.MSG_SUCCESS);
        resultModel.setData(pageInfo);
        return resultModel;
    }


    @Override
    public ResultModel<Boolean> delete(Times Times) {
        Times.setRecStatus(1);
        int row = timesMapper.update(Times);
        ResultModel resultModel = new ResultModel();
        if (1 == row) {
            resultModel.setCode(Commond.CODE_SUCCESS);
            resultModel.setMessage(Commond.MSG_SUCCESS);
            resultModel.setData(row);
        } else {
            resultModel.setCode(Commond.CODE_FAIL);
            resultModel.setMessage(Commond.MSG_FALE);
            resultModel.setData(row);
        }
        return resultModel;
    }

    @Override
    public ResultModel<String> selectEffectiveTime(String time, Integer timeSlot, Integer id, String AmOrPm) {
        long nm = 1000 * 60;
        //获取当前日期预约人数对应的时间集合
        Map mapRecord = new HashMap();
        mapRecord.put("beginTime", time + " 00:00:00");
        mapRecord.put("recordStatus", 1);
        List<TalksRecord> talksRecords = talksRecordMapper.selectList(mapRecord);

        //将预约人数对应的时间封装成map
        Map mapRecordKey = new HashMap();
        StringBuilder sbRecord = new StringBuilder();

        talksRecords
                .stream()
                .filter(talksRecord -> null != talksRecord)
                .forEach(talksRecord -> {
                            sbRecord.delete(0, sbRecord.length());
                            sbRecord.append(talksRecord.getBeginTime()).append("-").append(talksRecord.getEndTime());
                            mapRecordKey.put(sbRecord.toString(), talksRecord.getId());
                        }
                );

        //获取当前时间段对应的时间范围
        Times times = timesMapper.selectById(id);
        //时间差
        long diff;
        // 开始时间
        Timestamp bTime ;
        if ("am".equalsIgnoreCase(AmOrPm)) {
            // 上午
            diff = times.getAmEndTime().getTime() - times.getAmBeginTime().getTime();
            bTime = times.getAmBeginTime();
        } else {
            //下午
            diff = times.getPmEndTime().getTime() - times.getPmBeginTime().getTime();
            bTime = times.getAmBeginTime();
        }
        // 计算差多少分钟
        long min = diff / nm;
        // 计算循环多少次
        int count = Double.valueOf((String.valueOf(Math.ceil(min / timeSlot)))).intValue();

        StringBuilder sbTime = new StringBuilder();
        StringBuilder sbResult = new StringBuilder();
        for (int i = 0; i < count; i++) {
            sbTime.delete(0, sbTime.length());
            //将开始时间往后推timeSlot分钟
            Timestamp eTime = new Timestamp(bTime.getTime() + nm * Long.valueOf(timeSlot));
            sbTime.append(bTime).append("-").append(eTime);
            //判断记录中key不包含在新时间里的时候 加入集合
            if (!mapRecordKey.containsKey(sbTime.toString())) {
                sbResult.append(sbTime.toString()).append(",");
            }
            bTime = eTime;
        }
        ResultModel resultModel = new ResultModel();
        resultModel.setCode(Commond.CODE_SUCCESS);
        resultModel.setMessage(Commond.MSG_SUCCESS);
        resultModel.setData(sbResult.toString().substring(0, sbResult.toString().length() - 1));
        return resultModel;
    }
}
